package com.sskj.run.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.AssetBean;
import com.sskj.run.component.DaggerUserDataComponent;
import com.sskj.run.presenter.AssetFragmentPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.RUN_FRAGMENT_ASSET)//我的资产
public class AssetFragment extends BaseFragment<AssetFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter<AssetBean> slimAdapter;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.run_activity_asset;
    }

    @Override
    public AssetFragmentPresenter getPresenter() {
        return new AssetFragmentPresenter();
    }


    @Override
    protected void initView() {

        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getQCInfo();

            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.run_recy_item_asset, new SlimInjector<AssetBean>() {
            @Override
            public void onInject(AssetBean data, IViewInjector injector, List payloads) {

                injector
                        .visibility(R.id.btn_recharge, View.VISIBLE)
                        .visibility(R.id.btn_with_draw, View.VISIBLE)
                        .visibility(R.id.btn_exchange, View.GONE)
                        .image(R.id.iv_image_type, CoinEnum.getByCode(data.getName()).getRes())
                        .text(R.id.tv_money_type, data.getName())
                        .text(R.id.tv_money, data.getUsable())
                        .clicked(R.id.tv_bill_record, view -> ARouter.getInstance().build(RConfig.RUN_BILLIND_RECORD).withString(Constans.MONEY_TYPE, data.getName()).navigation())
                        .clicked(R.id.btn_recharge, view -> {
                            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                                return;
                            }
                            ARouter.getInstance().build(RConfig.RUN_TRANSFER)
                                    .withBoolean("isIn", true)
                                    .withString("id", data.getPid())
                                    .withString("money", data.getUsable())
                                    .withString(Constans.MONEY_TYPE, data.getName())
                                    .navigation();
                        })
                        .clicked(R.id.btn_with_draw, view -> {
                            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                                return;
                            }
                            ARouter.getInstance().build(RConfig.RUN_TRANSFER)
                                    .withBoolean("isIn", false)
                                    .withString("id", data.getPid())
                                    .withString("money", data.getUsable())
                                    .withString(Constans.MONEY_TYPE, data.getName())
                                    .navigation();
                        });

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        coolRefreshView.setRefreshing(true);

    }

    public void updateQC(List<AssetBean> assetBeans) {
        coolRefreshView.setRefreshing(false);
        slimAdapter.updateData(assetBeans);

    }

    @Subscribe(threadMode = ThreadMode.MAIN, code = RxBusCode.REFRESH_RUN_ASSET)
    public void refresh() {
        if (coolRefreshView != null) {
            coolRefreshView.setRefreshing(true);
        }
    }


}
